package com.ruyuan.commodity.common.mamager.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruyuan.commodity.common.mapper.GoodsGalleryMapper;
import com.ruyuan.commodity.common.mamager.GoodsInfoGalleryManager;
import com.ruyuan.commodity.model.goods.dos.GoodsGalleryDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品相册业务类
 */
@Service
public class GoodsInfoGalleryManagerImpl implements GoodsInfoGalleryManager {

    private static final Logger LOG = LoggerFactory.getLogger(GoodsInfoGalleryManagerImpl.class);

    /**
     * 商品相册的mapper组件
     */
    @Autowired
    private GoodsGalleryMapper goodsGalleryMapper;

    /**
     * 添加商品相册
     *
     * @param goodsGallery 商品相册
     * @return GoodsGallery 商品相册
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GoodsGalleryDO add(GoodsGalleryDO goodsGallery) {
        this.goodsGalleryMapper.insert(goodsGallery);
        return goodsGallery;
    }

    /**
     * 修改商品相册
     * @param goodsGallery 商品相册
     * @param id 商品相册主键
     * @return GoodsGallery 商品相册
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GoodsGalleryDO edit(GoodsGalleryDO goodsGallery, Long id) {
        goodsGallery.setImgId(id);
        this.goodsGalleryMapper.updateById(goodsGallery);
        return goodsGallery;
    }

    /**
     * 删除商品相册
     *
     * @param id 商品相册主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        this.goodsGalleryMapper.deleteById(id);
    }

    /**
     * 获取商品相册
     *
     * @param id 商品相册主键
     * @return GoodsGallery 商品相册
     */
    @Override
    public GoodsGalleryDO getModel(Long id) {
        return this.goodsGalleryMapper.selectById(id);
    }

    /**
     * 使用原始图片得到商品的其他规格的图片格式
     *
     * @param origin 图片原始路径
     * @return 商品相册实体
     */
    @Override
    public GoodsGalleryDO getGoodsGallery(String origin) {
        GoodsGalleryDO goodsGallery = new GoodsGalleryDO();
        // 缩略图上传
        //String thumbnail = uploadClient.getUrl(origin, SystemSettingConfig.THUMBNAIL, 300);
        // 小图上传
        //String small = uploadClient.getUrl(origin, 400, 400);
        // 大图大图上传
        //String big = uploadClient.getUrl(origin, 800, 800);
        // 赋值
        goodsGallery.setBig(origin);
        goodsGallery.setSmall(origin);
        goodsGallery.setThumbnail(origin);
        goodsGallery.setOriginal(origin);
        return goodsGallery;
    }

    /**
     * 添加商品的相册
     *
     * @param goodsGalleryList 图片集合
     * @param goodsId 商品id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(List<GoodsGalleryDO> goodsGalleryList, Long goodsId) {
        int i = 0;
        for (GoodsGalleryDO origin : goodsGalleryList) {
            // 获取带所有缩略的相册
            GoodsGalleryDO galley = this.getGoodsGallery(origin.getOriginal());
            galley.setGoodsId(goodsId);
            // 默认第一个为默认图片
            if (i == 0) {
                galley.setIsdefault(1);
            } else {
                galley.setIsdefault(0);
            }
            i++;
            this.add(galley);
        }
    }

    /**
     * 修改某商品的相册
     * @param goodsGalleryList 图片集合
     * @param goodsId 商品id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(List<GoodsGalleryDO> goodsGalleryList, Long goodsId) {
        LOG.info("更新商品相册的入参为 goodsGalleryList=[{}] 商品id=[{}]", JSONUtil.toJsonStr(goodsGalleryList), goodsId);
        // 删除没有用到的商品相册信息
        this.delNoUseGalley(goodsGalleryList, goodsId);
        //用于Mapper更新使用的对象
        GoodsGalleryDO galleryUpdateDO = new GoodsGalleryDO();
        int i = 0;
        // 如果前台传入id不为-1，则新增商品图片到此商品的相册中 添加相册
        for (GoodsGalleryDO goodsGallery : goodsGalleryList) {
            //已有图片且默认
            if (goodsGallery.getImgId() != -1 && i == 0) {
                //将此图片设置为默认
                galleryUpdateDO.setIsdefault(1);
                // 商品相册默认图片更新条件包装器
                UpdateWrapper<GoodsGalleryDO> galleryWrapper = new UpdateWrapper<GoodsGalleryDO>()
                        .eq("img_id", goodsGallery.getImgId());
                goodsGalleryMapper.update(galleryUpdateDO, galleryWrapper);
                // 将其他图片设置为不默认
                galleryUpdateDO.setIsdefault(0);
                // 商品相册非默认图片更新条件包装器
                UpdateWrapper<GoodsGalleryDO> galleryNoneDefaultWrapper = new UpdateWrapper<GoodsGalleryDO>()
                        .ne("img_id", goodsGallery.getImgId())
                        .eq("goods_id", goodsId);
                goodsGalleryMapper.update(galleryUpdateDO, galleryNoneDefaultWrapper);

                GoodsGalleryDO temp = this.getModel(goodsGallery.getImgId());

                LOG.info("商品的相册信息为 [{}]", JSONUtil.toJsonStr(temp));
                goodsGallery.setBig(temp.getBig());
                goodsGallery.setOriginal(temp.getOriginal());
                goodsGallery.setSmall(temp.getSmall());
                goodsGallery.setThumbnail(temp.getThumbnail());
                goodsGallery.setTiny(temp.getTiny());
            }
            // 新增的图片
            if (goodsGallery.getImgId() == -1) {
                // 获取带所有缩略的相册
                GoodsGalleryDO galley = this.getGoodsGallery(goodsGallery.getOriginal());
                galley.setGoodsId(goodsId);
                // 默认第一个为默认图片
                if (i == 0) {
                    galley.setIsdefault(1);
                    galleryUpdateDO.setIsdefault(0);
                    goodsGalleryMapper.update(galleryUpdateDO,
                            new UpdateWrapper<GoodsGalleryDO>()
                                    .eq("goods_id",goodsId));
                } else {
                    galley.setIsdefault(0);
                }
                goodsGalleryMapper.insert(galley);
                BeanUtils.copyProperties(galley, goodsGallery);
            }
            galleryUpdateDO.setIsdefault(null);
            galleryUpdateDO.setSort(i);
            goodsGalleryMapper.update(galleryUpdateDO,
                    new UpdateWrapper<GoodsGalleryDO>()
                            .eq("img_id",goodsGallery.getImgId()));
            i++;
        }
    }

    /**
     * 删除没有用到的商品相册信息
     *
     * @param galleryList 商品相册
     * @param goodsId     商品id
     */
    private void delNoUseGalley(List<GoodsGalleryDO> galleryList, Long goodsId) {
        // 将传入的商品图片id进行拼接
        List<Long> imgIds = galleryList.stream().map(GoodsGalleryDO::getImgId).collect(Collectors.toList());

        QueryWrapper<GoodsGalleryDO> galleryQueryWrapper = new QueryWrapper<GoodsGalleryDO>()
                .notIn("img_id", imgIds)
                .eq("goods_id", goodsId);

        // 删除掉不在此商品相册中得图片
        this.goodsGalleryMapper.delete(galleryQueryWrapper);
    }

    /**
     * 查询某商品的相册
     * @param goodsId 商品id
     * @return 商品相册列表
     */
    @Override
    public List<GoodsGalleryDO> list(Long goodsId) {
        QueryWrapper<GoodsGalleryDO> goodsGalleryDOQueryWrapper = new QueryWrapper<GoodsGalleryDO>()
                // 查询商品id
                .eq("goods_id", goodsId)
                // 按sort正序
                .orderByAsc("sort")
                // 按是否默认图片倒序
                .orderByDesc("isdefault");
        return this.goodsGalleryMapper.selectList(goodsGalleryDOQueryWrapper);
    }

    /**
     * 删除商品关联的相册
     * @param goodsIds 商品id数组
     */
    @Override
    public void delete(Long[] goodsIds) {
        QueryWrapper<GoodsGalleryDO> goodsGalleryQueryWrapper = new QueryWrapper<GoodsGalleryDO>()
                .in("goods_id", Arrays.asList(goodsIds));
        this.goodsGalleryMapper.delete(goodsGalleryQueryWrapper);
    }

}
